package br.com.plenarionews;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import br.com.plenarionews.model.Noticia;

public class NoticiasAdapter extends BaseAdapter{

	List<Noticia> data;
	Context context;
	
	public NoticiasAdapter(List<Noticia> data, Context context) {
		this.data = data;
		this.context = context;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = vi.inflate(R.layout.list_item_noticias, parent, false);

		TextView tituloView = (TextView) v.findViewById(R.id.titulo);
		TextView dataHoraView = (TextView) v.findViewById(R.id.dataHora);
		TextView fonteNoticiaView = (TextView) v.findViewById(R.id.fonteNoticia);

		final Noticia noticia = data.get(position);
		tituloView.setText(noticia.getTitulo());
		dataHoraView.setText(noticia.getDataHora());
		if (noticia.getFonteNoticia()!=null) {
			fonteNoticiaView.setText(noticia.getFonteNoticia().getNome()+ "  ");
		} else {
			fonteNoticiaView.setText("Fonte indefinida! ");
		}
		
		tituloView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(noticia.getLink())));
			}
		});

		return v;
	}

}
